import wx
import os
import wx.lib.mixins.listctrl  as  listmix


class RotationDistanceOrdered(wx.Panel):
    
    def __init__(self, parent, model):
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
        self.parent = parent
        self.model = model

        self.photoMaxSize = 500
        self.indexFirst = 0
        self.indexSecond = 0

        self.nodesListFirst = EditableListCtrl(self, ID=1, style=wx.LC_REPORT)

        self.nodesListFirst.InsertColumn(0, "Label", width=50)
        self.nodesListFirst.InsertColumn(1, "Parent", width=50)
        self.nodesListFirst.InsertColumn(2, "Child 1", width=50)
        self.nodesListFirst.InsertColumn(3, "Child 2", width=50)

        self.nodesListSecond = EditableListCtrl(self, ID=1, style=wx.LC_REPORT)

        self.nodesListSecond.InsertColumn(0, "Label", width=50)
        self.nodesListSecond.InsertColumn(1, "Parent", width=50)
        self.nodesListSecond.InsertColumn(2, "Child 1", width=50)
        self.nodesListSecond.InsertColumn(3, "Child 2", width=50)
        
        save = wx.Button(self, wx.ID_ANY, 'Save')
        addFirst = wx.Button(self, wx.ID_ANY, 'Add')
        removeFirst = wx.Button(self, wx.ID_ANY, 'Remove')
        addSecond = wx.Button(self, wx.ID_ANY, 'Add')
        removeSecond = wx.Button(self, wx.ID_ANY, 'Remove')
        save.Bind(wx.EVT_BUTTON, self.OnSave)
        addFirst.Bind(wx.EVT_BUTTON, self.OnAddFirst)
        removeFirst.Bind(wx.EVT_BUTTON, self.OnRemoveFirst)
        addSecond.Bind(wx.EVT_BUTTON, self.OnAddFirst)
        removeSecond.Bind(wx.EVT_BUTTON, self.OnRemoveFirst)
        

        img = wx.EmptyImage(500, 560)
        self.first = wx.StaticBitmap(self, wx.ID_ANY,
                                     wx.BitmapFromImage(img))

        img = wx.EmptyImage(500, 560)
        self.second = wx.StaticBitmap(self, wx.ID_ANY,
                                      wx.BitmapFromImage(img))

        sizer = wx.BoxSizer(wx.VERTICAL)

        steps = wx.BoxSizer(wx.HORIZONTAL)

        nextStep = wx.Button(self, wx.ID_ANY, 'Next')
        nextStep.Bind(wx.EVT_BUTTON, self.OnNext)

        run = wx.Button(self, wx.ID_ANY, 'Run')
        run.Bind(wx.EVT_BUTTON, self.OnRun)

        steps.Add(nextStep, 0, wx.ALL, 5)
        steps.Add(run, 0, wx.ALL, 5)

        

        buttonsFirst = wx.BoxSizer(wx.HORIZONTAL)
        buttonsFirst.Add(addFirst, 0, wx.ALL, 5)
        buttonsFirst.Add(removeFirst, 0, wx.ALL, 5)

        buttonsSecond = wx.BoxSizer(wx.HORIZONTAL)
        buttonsSecond.Add(addSecond, 0, wx.ALL, 5)
        buttonsSecond.Add(removeSecond, 0, wx.ALL, 5)

        edit = wx.BoxSizer(wx.VERTICAL)
        edit.Add(self.nodesListFirst, 0, wx.ALL, 5)
        edit.Add(buttonsFirst, 0, wx.ALL, 0)
        edit.Add(self.nodesListSecond, 0, wx.ALL, 5)
        edit.Add(buttonsSecond, 0, wx.ALL, 0)
        edit.Add(save, 0, wx.ALL, 5)
        
        first = wx.BoxSizer(wx.VERTICAL)
        first.Add(self.first, 0, wx.ALL, 5)
        
        second = wx.BoxSizer(wx.VERTICAL)
        second.Add(self.second, 0, wx.ALL, 5)

        display = wx.BoxSizer(wx.HORIZONTAL)

        display.Add(edit, 0, wx.ALL, 5)
        display.Add(first, 0, wx.ALL, 5)
        display.Add(second, 0, wx.ALL, 5)


        sizer.Add(display, 0, wx.ALL, 5)
        sizer.Add(steps, 0, wx.ALL, 5)

        self.SetSizer(sizer)

    def loadImageFirst(self, image):
        image_name = os.path.basename(image)
        img = wx.Image(image, wx.BITMAP_TYPE_ANY)

        W = img.GetWidth()
        H = img.GetHeight()
        if W > H:
            NewW = self.photoMaxSize
            NewH = self.photoMaxSize * H / W
        else:
            NewH = self.photoMaxSize
            NewW = self.photoMaxSize * W / H
        img = img.Scale(NewW,NewH)

        self.first.SetBitmap(wx.BitmapFromImage(img))
        self.Refresh()

    def loadImageSecond(self, image):
        image_name = os.path.basename(image)
        img = wx.Image(image, wx.BITMAP_TYPE_ANY)

        self.second.SetBitmap(wx.BitmapFromImage(img))
        self.Refresh()

    def update(self):
        return

    def OnAddFirst(self, event):
        self.nodesListFirst.InsertStringItem(self.indexFirst, "A")
        self.nodesListFirst.SetStringItem(self.indexFirst, 1, "A")
        self.nodesListFirst.SetStringItem(self.indexFirst, 2, "A")
        self.nodesListFirst.SetStringItem(self.indexFirst, 3, "A")
        self.indexFirst = self.indexFirst + 1
        


    def OnAddSecond(self, event):
        self.nodesListSecond.InsertStringItem(self.indexSecond, "A")
        self.nodesListSecond.SetStringItem(self.indexSecond, 1, "A")
        self.nodesListSecond.SetStringItem(self.indexSecond, 2, "A")
        self.nodesListSecond.SetStringItem(self.indexSecond, 3, "A")
        self.indexSecond = self.indexSecond + 1

    def OnRemoveFirst(self, event):
        return

    def OnRemoveSecond(self, event):
        return

    def OnSave(self, event):
        self.loadImageFirst("C:\\Users\\Charles\\Documents\\jira-reporting\\Thesis\\Images\\test1.png")
        return

    def OnNext(self, event):
        return

    def OnRun(self, event):
        return

##############################################################################

class EditableListCtrl(wx.ListCtrl, listmix.TextEditMixin):
    ''' TextEditMixin allows any column to be edited. '''
 
    #----------------------------------------------------------------------
    def __init__(self, parent, ID=wx.ID_ANY, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=0):
        """Constructor"""
        wx.ListCtrl.__init__(self, parent, ID, pos, (200, 240), style)
        listmix.TextEditMixin.__init__(self)
    

        
